//
//  FaceChooseView.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/8/2.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit
import SnapKit

class FaceChooseView: UIView {
    
    
    //懒加载控件
    private lazy var faceColView: FaceCollectionView = FaceCollectionView()
    private lazy var faceSwithView: FaceSwithView = FaceSwithView()
    
    //懒加载页数控制器
    private lazy var pageControl: UIPageControl = {
        
        let pageCont = UIPageControl()
        
        //swift里没有成员变量,只有属性,只能通过kvc设置图片
        pageCont.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pageCont.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        
        //如果只有一页就影藏
        pageCont.hidesForSinglePage = true
        
        return pageCont
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 界面设置
    func setupUI(){
        
        //设置分页控制器起始第二组的显示
        let indexp = IndexPath(item: 0, section: 1)
        setPageControlPage(indexPath: indexp)
        
        addSubview(faceColView)
        addSubview(pageControl)
        addSubview(faceSwithView)
        
        faceColView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.height.equalTo(180)
        }
        faceColView.pageControl = pageControl
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(faceColView.snp.bottom)
//            make.width.equalTo(10)
            make.height.equalTo(10)
        }
        faceSwithView.snp.makeConstraints { (make) in
            make.top.equalTo(pageControl.snp.bottom)
            make.leading.trailing.bottom.equalTo(self)
        }
        
        //col联动tool
        faceColView.linkageFaceSwithClusure = {[unowned self] (indexPath) in
            
            self.faceSwithView.selectButton(indexPath: indexPath)
        }
        
        //闭包赋值
        faceSwithView.clickFaceSwithButtonClusure = {[unowned self] (type) in
            
            let indexPath = IndexPath(item: 0, section: type.rawValue - 10)
            
            self.faceColView.scrollToItem(at: indexPath, at: .left, animated: true)
        }
        
    }
    
       
    //MARK: - 分页控制器页数
    func setPageControlPage(indexPath: IndexPath){
        
        pageControl.numberOfPages = FaceViewModel.shareModel.allArray[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
    }
}
